package cn.patient.pp.module.patients.framework.web.config.async;

import cn.patient.pp.framework.common.util.object.BeanUtils;
import cn.patient.pp.module.patients.controller.admin.vo.importExecl.OperationsLogVO;
import cn.patient.pp.module.patients.dal.dataobject.patients.OperationsLogDO;
import cn.patient.pp.module.patients.dal.dataobject.patients.PatientsDODO;
import cn.patient.pp.module.patients.dal.mysql.patients.PatientsDOMapper;
import cn.patient.pp.module.patients.framework.web.config.Config;
import cn.patient.pp.module.patients.service.OperationsLogService;
import cn.patient.pp.module.report.dal.mysql.report.ReportMapper;
import cn.patient.pp.module.report.service.PatientInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author liqingsong
 * @version 1.0
 * Create by 2025/4/26
 * 写一些异步任务
 */
@Service
public class AsyncTaskServieImpl implements  AsyncTaskServie{
    @Resource
    private PatientsDOMapper dOMapper;
    @Resource
    private OperationsLogService operationsLogService;
    @Resource
    private ReportMapper reportMapper;


    /**
     * 异步删除patientCode18个0
     * 每次导入execl之后，异步调用，判断patientCode18个0是否在表里有基本信息数据，
     * 有的话则删除，代表这个人的信息已经从省厅导入到了基本信息里面
     */
    @Async
    @Override
    public void deletePatientWithZero(){
        //查询出所有patient是18个0的用户数据
        List<PatientsDODO> patientCode = dOMapper.selectList("patient_code", Config.HAS_DIAGNOSED_PATIENT_CODE);
        Set<String> idNumber = patientCode.stream().map(PatientsDODO::getIdNumber).collect(Collectors.toSet());

        //去数据库查询这些身份证的数据
        LambdaQueryWrapper<PatientsDODO> patientsDODOLambdaQueryWrapper = new LambdaQueryWrapper<>();

        //查询这些身份证,且是18个0的所有相关数据，这些数据是要删除的数据
        patientsDODOLambdaQueryWrapper.in(PatientsDODO::getIdNumber, idNumber)
                .ne(PatientsDODO::getPatientCode,Config.HAS_DIAGNOSED_PATIENT_CODE);
        //查询需要修改状态的数据
        List<PatientsDODO> Patients = dOMapper.selectList(patientsDODOLambdaQueryWrapper);
        if (Patients!=null && !Patients.isEmpty()){

            Set<String> updateIdNumber = Patients.stream()
                    .map(patient -> patient.getIdNumber().toString())
                    .collect(Collectors.toSet());

            if (updateIdNumber!=null && !updateIdNumber.isEmpty()){
                //修改数据表,上报状态：1-已上报县级，2-待上报，3-数据待完善，4-数据上报成功，
                Integer i1 = reportMapper.batchUpdatePatients(updateIdNumber, "4");
                //基本信息表删除这些数据
                Integer i2 = dOMapper.deleteByIdsAndPatientCode(updateIdNumber,Config.HAS_DIAGNOSED_PATIENT_CODE);
                //插入日志表
                ArrayList<OperationsLogVO> patientVOS = new ArrayList<>();
                for (PatientsDODO patient : Patients) {
                    if (i1>0){
                        OperationsLogVO operationsLogVO = new OperationsLogVO();
                        operationsLogVO.setPatientName(patient.getPatientName());
                        operationsLogVO.setPatientCode(patient.getPatientCode());
                        operationsLogVO.setIdNumber(patient.getIdNumber());
                        operationsLogVO.setAction("数据上报成功");
                        operationsLogVO.setDetailsData(patient.toString());
                        patientVOS.add(operationsLogVO);
                    }
                    if (i2>0){
                        OperationsLogVO operationsLogVO = new OperationsLogVO();
                        operationsLogVO.setPatientName(patient.getPatientName());
                        operationsLogVO.setPatientCode(patient.getPatientCode());
                        operationsLogVO.setIdNumber(patient.getIdNumber());
                        operationsLogVO.setAction("删除基本信息表成功");
                        operationsLogVO.setDetailsData(patient.toString());
                        patientVOS.add(operationsLogVO);
                    }
                }
                operationsLogService.insertBatch(patientVOS);
            }

        }

    }
}

